package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.ArticleManagement;
import com.ruoyi.system.mapper.ArticleManagementMapper;
import com.ruoyi.system.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Base64;
import java.util.List;

import static com.ruoyi.common.core.domain.AjaxResult.error;

/***
 * 说明
 * @author:ZZ
 * @date: 2024-12-05  22:13
 **/

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleManagementMapper,ArticleManagement> implements ArticleService {



    //图片上传路径
    @Value(value = "${path.int_img}")
    private String INT_IMG_PATH;
    //上传图片的访问路径
    @Value(value = "${path.out_img}")
    private String OUT_IMG_URL;

    @Value(value = "${path.http_url}")
    private String OUT_HTTP_URL;

    @Autowired
    private ArticleManagementMapper mapper;

    @Override
    public Object saveArticle(ArticleManagement articleManagement) throws IOException {
        //附件信息不为空  图片信息写入指定文件夹  1
        String imgUrl = INT_IMG_PATH;
        String imgPath = OUT_IMG_URL;
        //将封面图片转为Base64
        if (null != articleManagement.getFile()) {
            FileUtil(articleManagement, imgPath, imgUrl);
//            MultipartFile file = articleManagement.getFile();
//            String imgBase = EncodeFileToBase64(file);
//            articleManagement.setImgBase(imgBase);
        }
        String time = DateUtils.getDate();
        articleManagement.setTime(time);
        int falg = mapper.insert(articleManagement);
        if (falg >0){
            return  new BaseController().success("添加成功");
        }
        return  new BaseController().error("添加失败");
    }


    public String EncodeFileToBase64(MultipartFile file) {
        try {
            // 读取文件内容到字节数组
            byte[] bytes = file.getBytes();
            // 使用Base64编码器将字节数组编码为Base64字符串
            String base64String = Base64.getEncoder().encodeToString(bytes);
            return base64String;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int removeFoodStatus(List<Long> ids) {
        mapper.removeFoodStatus(ids);
        return 0;
    }

    @Override
    public int updateByDetail(ArticleManagement management) throws IOException {
        //附件信息不为空  图片信息写入指定文件夹
//        if (null != management.getFile()) {
//            String imgUrl = INT_IMG_PATH;
//            String imgPath = OUT_IMG_URL;
////            FileUtil(management, imgPath, imgUrl);
//
//        }
        //将封面图片转为Base64
//        if (null != management.getFile()) {
//            management.setImgBase(null);
////            FileUtil(articleManagement, imgPath, imgUrl);
//            MultipartFile file = management.getFile();
//            String imgBase = EncodeFileToBase64(file);
//            management.setImgBase(imgBase);
//        }
                    String imgUrl = INT_IMG_PATH;
            String imgPath = OUT_IMG_URL;
        //将封面图片转为Base64
        if (null != management.getFile()) {
            FileUtil(management, imgPath, imgUrl);
//            MultipartFile file = articleManagement.getFile();
//            String imgBase = EncodeFileToBase64(file);
//            articleManagement.setImgBase(imgBase);
        }
        UpdateWrapper<ArticleManagement> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",management.getId());
        mapper.update(management,wrapper);
        return 0;
    }

    public void FileUtil(ArticleManagement orderFoodDetail, String imgPath, String imgUrl) throws IOException {
        if (null != orderFoodDetail.getFile()) {
             // 获取登录信息
//            Long userId = SecurityUtils.getUserId();
//            // 图片存储路径
//            imgPath += userId + "/";
//            imgUrl += userId + "/";
            // 上传文件名称
//            orderFoodDetail.setImgName(orderFoodDetail.getFile().getOriginalFilename());
            //生成图片名称UUID
            String imgName = UUID.fastUUID().toString();
            String  nameStr = imgName;
            imgPath += imgName;
            imgUrl += imgName;
            //图片写入文件夹

            MultipartFile file = orderFoodDetail.getFile();
            if (file.getContentType().contains("image/png") || file.getContentType().contains("png") || file.getContentType().contains("PNG")) {
                imgPath += ".png";
                imgUrl += ".png";
                nameStr += ".png";
            } else if (file.getContentType().contains("jpg") ||file.getContentType().contains("JPG") || file.getContentType().contains("image/jpeg")) {
                imgPath += ".jpg";
                imgUrl += ".jpg";
                nameStr += ".jpg";
            }else {
                imgPath += ".png";
                imgUrl += ".png";
                nameStr += ".png";
            }

            if (null != imgPath && !imgPath.isEmpty()) {
                File files = new File(imgPath);
                files.mkdirs();
                file.transferTo(files);
                orderFoodDetail.setImgPath(imgUrl);
                orderFoodDetail.setHttpPath(OUT_HTTP_URL+nameStr);
            }
        }
    }

}
